import { defineStore } from 'pinia'

export const useAuthStore = defineStore('auth', {
  state: () => ({
    user: null as null | { id: number; phone: string; createdAt: string },
    loading: false,
  }),
  actions: {
    async fetchMe() {
      this.loading = true
      try {
        const { user } = await $fetch('/api/auth/me')
        this.user = user ?? null
      } catch {
        this.user = null
      } finally {
        this.loading = false
      }
    },
    async logout() {
      await $fetch('/api/auth/logout', { method: 'POST' })
      this.user = null
      await navigateTo('/auth/login')
    }
  }
})


